﻿using ModernShopping.AppCode.WebUtil;
using SiteUtils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ModernShopping.AppCode
{
    public class DbProduct
    {
        public static bool Add(Product product)
        {
            try
            {
                ModernShoppingDataContext db = new ModernShoppingDataContext();
                product.IsDeleted = false;
                db.Products.InsertOnSubmit(product);
                db.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static bool Update(Product product)
        {
            try
            {
                ModernShoppingDataContext db = new ModernShoppingDataContext();
                Product temp = db.Products.Where(c => c.Code.Equals(product.Code)).FirstOrDefault();
                temp.Name = product.Name;
                temp.Price = product.Price;
                temp.Description = product.Description;
                temp.MadeIn = product.MadeIn;
                temp.ImageUrl = product.ImageUrl;
                db.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static bool Delete(int ProductCode)
        {
            try
            {
                ModernShoppingDataContext db = new ModernShoppingDataContext();
                Product temp = db.Products.Where(c => c.Code.Equals(ProductCode)).FirstOrDefault();
                temp.IsDeleted = true;
                db.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static bool UpdateImage(int ProductCode, string Path)
        {
            try
            {
                ModernShoppingDataContext db = new ModernShoppingDataContext();
                Product temp = db.Products.Where(c => c.Code.Equals(ProductCode)).First();
                temp.ImageUrl = Path;
                db.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static Product GetByCode(int Code)
        {
            try
            {
                ModernShoppingDataContext db = new ModernShoppingDataContext();
                return db.Products.Where(c => c.Code.Equals(Code) && c.IsDeleted == false).FirstOrDefault();
            }
            catch
            {
                return null;
            }
        }

        public static void initDatalist(DataList dl, CollectionPager CollectionPager, Guid CompanyCode)
        {
            ModernShoppingDataContext db = new ModernShoppingDataContext();
            var results = db.Products
                            .Where(c => c.CompanyCode.Equals(CompanyCode) && c.IsDeleted == false)
                            .Select(p =>
                                new
                                {
                                    ProductCode = p.Code,
                                    Name = p.Name,
                                    Price = p.Price,
                                    ImageUrl = p.ImageUrl,
                                    PostBackUrl = string.Format("~/ProductInfo.aspx?ProductCode={0}", p.Code)
                                });
            CollectionPager.DataSource = results.ToList();
            CollectionPager.BindToControl = dl;
            dl.DataSource = CollectionPager.DataSourcePaged;
            dl.DataBind();
        }

        public static string GetQrURL(int ProductCode)
        {
            ModernShoppingDataContext db = new ModernShoppingDataContext();
            var od = db.Products.Where(c => c.Code.Equals(ProductCode)).FirstOrDefault();
            return od != null ? od.QrCode : null;
        }

        public static bool UpdateQRCode(int ProductCode, string QRPath)
        {
            try
            {
                ModernShoppingDataContext db = new ModernShoppingDataContext();
                Product temp = db.Products.Where(c => c.Code.Equals(ProductCode)).First();
                temp.QrCode = QRPath;
                db.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static void initDatalist(DataList dl, CollectionPager CollectionPager, string Keywords)
        {
            ModernShoppingDataContext db = new ModernShoppingDataContext();
            var results = db.Products
                            .Where(c => c.IsDeleted == false)
                            .Where(c => c.Name.ToLower().Contains(Keywords))
                            .Select(p =>
                                new
                                {
                                    ProductCode = p.Code,
                                    Name = p.Name,
                                    Price = p.Price,
                                    ImageUrl = p.ImageUrl,
                                    PostBackUrl = string.Format("~/ProductInfo.aspx?ProductCode={0}", p.Code)
                                });
            CollectionPager.DataSource = results.ToList();
            CollectionPager.BindToControl = dl;
            dl.DataSource = CollectionPager.DataSourcePaged;
            dl.DataBind();
        }
    }
}